<template>
    <div class="artist">
        <!-- 歌手列表 -->
        <div class="artist_list">
            <ul>
                <li v-for="(item, index) in artists" :key="index" @click="$router.push(`/artlist/${item.id}`)">
                    <div class="header">
                        <img :src="item.picUrl" alt="" style="width:130px; height:130px;">
                        <i :title="item.name+'的音乐'" class="mask"></i>
                    </div>
                    <div class="info tanxin">
                        <span class="ellipsis" :title="item.name+'的音乐'" v-html="highlight(item.name)"></span>
                        <i :title="item.name+'的个人主页'" class="people"></i>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
import { ArtistSearchApi } from "@/request/api";
export default {
    data () {
        return {
            artists: [],
        }
    },
    computed: {
        keyword() {
            return this.$route.params.keyword;
        }
    },
    watch: {
        keyword: {
            handler(newValue, oldValue){
                if(newValue !== oldValue){
                    ArtistSearchApi(newValue).then((res) => {
                        // console.log(res);
                        this.artists = res.data.result.artists;
                    });
                }
            },
            immediate: true,
        }
    },
    // created() {
    //     ArtistSearchApi(this.keyword).then((res) => {
    //         // console.log(res);
    //         this.artists = res.data.result.artists;
    //     });
    // },
    methods: {
        highlight(str) {
            // 正则表达式//中间的字符只是字符串
            // 如果需要动态创建一个正则表达式
            // new RegExp(str, rules) str是正则字符串，rules是匹配模式写在字符串中
            const reg = new RegExp(this.keyword, 'gi')
            const highStr = str.replace(reg, `<span style="color: #0c73c2;">${this.keyword}</span>`)
            return highStr
        },
    }
}
</script>
 
<style lang = "less" scoped>
@import "../../assets/base.less";
.artist {
    .artist_list {
        ul {
            flex-wrap: wrap;
            display: flex;
            justify-content: flex-start !important;
        }
        li {
            width: 130px;
            height: 150px;
            padding: 0 28px 30px 0;
            &:nth-of-type(6n) {
                padding-right: 0;
            }
            .header {
                position: relative;
                height: 130px;
                width: 130px;
                overflow: hidden;
                .mask {
                    cursor: pointer;
                    position: absolute;
                    top: 0;
                    left: 0;
                    height: 130px;
                    width: 130px;
                    background: url("../../assets/images/public/coverall.png") no-repeat -10px 0px;
                }
                &:hover {
                    img {
                        transform: scale(1.2);
                    }
                }
            }
            .info {
                padding-top: 5px;
                font-size: 12px;
                display: flex;
                align-items: center;
                span {
                    &:hover {
                        cursor: pointer;
                        text-decoration: underline;
                    }
                }
                .people {
                    cursor: pointer;
                    width: 18px;
                    height: 18px;
                    display: block;
                    background: url("../../assets/images/public/icon.png") no-repeat 0px -739px;
                }
            }
        }
    }
}
</style>